package com.particle.data.app.company.executor.warehouse;

import com.particle.common.app.executor.AbstractBaseExecutor;
import com.particle.data.app.company.executor.DataCompanyIprPatentContentCreateCommandExecutor;
import com.particle.data.app.company.executor.DataCompanyIprPatentContentUpdateCommandExecutor;
import com.particle.data.app.company.executor.representation.exwarehouse.DataCompanyIprPatentContentExWarehouseCommandExecutor;
import com.particle.data.client.company.dto.command.DataCompanyIprPatentContentCreateCommand;
import com.particle.data.client.company.dto.command.DataCompanyIprPatentContentUpdateCommand;
import com.particle.data.client.company.dto.command.representation.exwarehouse.DataCompanyIprPatentContentExWarehouseQueryCommand;
import com.particle.data.client.company.dto.command.warehouse.DataCompanyIprPatentContentWarehouseCommand;
import com.particle.data.client.company.dto.data.exwarehouse.DataCompanyIprPatentContentExWarehouseVO;
import com.particle.data.infrastructure.company.service.IDataCompanyIprPatentContentService;
import com.particle.global.dto.response.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 企业知识产权专利内容入库 指令执行器
 * </p>
 *
 * @author yw
 * @since 2025-04-07 11:14:10
 */
@Component
@Validated
public class DataCompanyIprPatentContentWarehouseCommandExecutor extends AbstractBaseExecutor {

	private DataCompanyIprPatentContentExWarehouseCommandExecutor dataCompanyIprPatentContentExWarehouseCommandExecutor;
	private DataCompanyIprPatentContentCreateCommandExecutor dataCompanyIprPatentContentCreateCommandExecutor;
	private IDataCompanyIprPatentContentService iDataCompanyIprPatentContentService;
	private DataCompanyIprPatentContentUpdateCommandExecutor dataCompanyIprPatentContentUpdateCommandExecutor;


	/**
	 * 企业知识产权专利内容入库
	 * @param dataCompanyIprPatentContentWarehouseCommand
	 * @return
	 */
	public SingleResponse<DataCompanyIprPatentContentExWarehouseVO> warehouse(DataCompanyIprPatentContentWarehouseCommand dataCompanyIprPatentContentWarehouseCommand) {
		DataCompanyIprPatentContentExWarehouseQueryCommand dataCompanyIprPatentContentExWarehouseQueryCommand = DataCompanyIprPatentContentExWarehouseQueryCommand.create(dataCompanyIprPatentContentWarehouseCommand.getCompanyIprPatentId());
		SingleResponse<DataCompanyIprPatentContentExWarehouseVO> dataCompanyIprPatentContentExWarehouseVOSingleResponse = dataCompanyIprPatentContentExWarehouseCommandExecutor.exWarehouse(dataCompanyIprPatentContentExWarehouseQueryCommand);
		DataCompanyIprPatentContentExWarehouseVO dataCompanyIprPatentContentExWarehouseVO = dataCompanyIprPatentContentExWarehouseVOSingleResponse.getData();
		// 不存在，添加
        if (dataCompanyIprPatentContentExWarehouseVO == null) {
			DataCompanyIprPatentContentCreateCommand dataCompanyIprPatentContentCreateCommand = DataCompanyIprPatentContentCreateCommand.createByWarehouseCommand(dataCompanyIprPatentContentWarehouseCommand);
			dataCompanyIprPatentContentCreateCommandExecutor.execute(dataCompanyIprPatentContentCreateCommand);
			// 新增后重新查询，返回最新数据
			dataCompanyIprPatentContentExWarehouseVOSingleResponse = dataCompanyIprPatentContentExWarehouseCommandExecutor.exWarehouse(dataCompanyIprPatentContentExWarehouseQueryCommand);
			return dataCompanyIprPatentContentExWarehouseVOSingleResponse;
		}else {
			// 	存在，尝试入库
			// 仅更新有变化的字段，将相同的字段设置为null不更新
			dataCompanyIprPatentContentWarehouseCommand.compareAndSetNullWhenEquals(dataCompanyIprPatentContentExWarehouseVO);

			// 判断是否所有字段都为空，所有字段都没有变化，不需要更新
			if (dataCompanyIprPatentContentWarehouseCommand.allFieldEmpty()) {
				// 更新最后处理时间
				iDataCompanyIprPatentContentService.updateLatestHandleAt(dataCompanyIprPatentContentExWarehouseVO.getId());
				// 如果没有需要更新的字段，则直接返回
				return dataCompanyIprPatentContentExWarehouseVOSingleResponse;
			} else {
				// 更新处理
				DataCompanyIprPatentContentUpdateCommand dataCompanyIprPatentContentUpdateCommand = DataCompanyIprPatentContentUpdateCommand.createByWarehouseCommand(
						dataCompanyIprPatentContentExWarehouseVO.getId(),
						dataCompanyIprPatentContentExWarehouseVO.getVersion(),
						dataCompanyIprPatentContentWarehouseCommand
				);
				dataCompanyIprPatentContentUpdateCommandExecutor.execute(dataCompanyIprPatentContentUpdateCommand);
			}

			// 更新完成后，新增的情况已经在新增逻辑里面直接返回了，查询返回
			dataCompanyIprPatentContentExWarehouseVOSingleResponse = dataCompanyIprPatentContentExWarehouseCommandExecutor.exWarehouse(dataCompanyIprPatentContentExWarehouseQueryCommand);
			return dataCompanyIprPatentContentExWarehouseVOSingleResponse;
		}
	}

	@Autowired
	public void setDataCompanyIprPatentContentExWarehouseCommandExecutor(DataCompanyIprPatentContentExWarehouseCommandExecutor dataCompanyIprPatentContentExWarehouseCommandExecutor) {
		this.dataCompanyIprPatentContentExWarehouseCommandExecutor = dataCompanyIprPatentContentExWarehouseCommandExecutor;
	}
	@Autowired
	public void setDataCompanyIprPatentContentCreateCommandExecutor(DataCompanyIprPatentContentCreateCommandExecutor dataCompanyIprPatentContentCreateCommandExecutor) {
		this.dataCompanyIprPatentContentCreateCommandExecutor = dataCompanyIprPatentContentCreateCommandExecutor;
	}
	@Autowired
	public void setiDataCompanyIprPatentContentService(IDataCompanyIprPatentContentService iDataCompanyIprPatentContentService) {
		this.iDataCompanyIprPatentContentService = iDataCompanyIprPatentContentService;
	}
	@Autowired
	public void setDataCompanyIprPatentContentUpdateCommandExecutor(DataCompanyIprPatentContentUpdateCommandExecutor dataCompanyIprPatentContentUpdateCommandExecutor) {
		this.dataCompanyIprPatentContentUpdateCommandExecutor = dataCompanyIprPatentContentUpdateCommandExecutor;
	}
}
